package com.mvcplus.system.controller;

import com.mvcplus.common.constant.GlobalConstant;
import com.mvcplus.common.controller.BaseController;
import com.mvcplus.common.model.SessionInfo;
import com.mvcplus.common.utils.Datatable;
import com.mvcplus.system.model.SysUser;
import com.mvcplus.system.service.ISysOrganizationService;
import com.mvcplus.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by BINGO on 2016/10/27.
 * 系统用户信息
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("sysUser")
public class SysUserController extends BaseController {

    private final ISysUserService sysUserService;

    private final ISysOrganizationService organizationService;

    @Autowired
    public SysUserController(ISysUserService sysUserService, ISysOrganizationService organizationService) {
        this.sysUserService = sysUserService;
        this.organizationService = organizationService;
    }

    @RequestMapping("/")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("system/user");
        modelAndView.addObject("orgList",organizationService.selectList(null));
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/datatable")
    public Object datatable(HttpServletRequest request,SysUser sysUser){
        Integer draw = 1;
        Map<String, String[]> params = request.getParameterMap();
        for (String attr : params.keySet()) {
            String[] val = params.get(attr);
            // System.out.println(attr+"="+Arrays.toString(val));
			/*if (attr.equals("start"))
				start = Integer.parseInt(val[0]);
			if (attr.equals("length"))
				length = Integer.parseInt(val[0]);
			if (attr.equals("search[value]"))
				pattern = val[0];*/
            if (attr.equals("draw"))
                draw = Integer.parseInt(val[0]);
        }

        Datatable dt= new Datatable(draw + 1,sysUserService.selectList(null));

        return dt;
    }

    @RequestMapping("/addPage")
    public ModelAndView preSave(ModelAndView modelAndView, @RequestParam(value = "id", required = false) Long id) {
        modelAndView.setViewName("system/addUser");
        modelAndView.addObject("orgList",organizationService.selectList(null));
        if (id != null) {
            modelAndView.addObject("user", sysUserService.selectById(id));
        }
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("save")
    public Object save(SysUser sysUser) {

        SysUser user = sysUserService.selectOne(sysUser);
        if (user!=null){
            return renderError("用户名已存在!");
        }else{
            return sysUserService.insertSelective(sysUser) ? renderSuccess("添加成功") : renderError("添加失败");
        }
    }

    @ResponseBody
    @RequestMapping("update")
    public Object update(SysUser sysUser){
        return sysUserService.updateSelectiveById(sysUser) ? renderSuccess("修改成功") : renderError("修改失败");
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Object delete(@RequestParam(value = "id", required = false) Long id) {
        return sysUserService.deleteById(id) ? renderSuccess("删除成功") : renderError("删除失败");
    }

    /*修改密码页面*/
    @RequestMapping("/editPwdPage")
    public ModelAndView editPwdPage() {
        return new ModelAndView("system/userEditPwd");
    }

    @RequestMapping("/editUserPwd")
    @ResponseBody
    public Object editUserPwd(HttpServletRequest request,String oldPwd, String newPwd) {
        SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
        return sysUserService.editUserPwd(sessionInfo, oldPwd, newPwd) ? renderSuccess("删除成功") : renderError("删除失败");
    }

}